#include "dataclass.h"
#include <fstream>
#include <iostream>
#include <QDebug>

using namespace std;


//在主程序中需要做的事情：调用DataClass::findMaxMinData(int begin, int end)函数
//判断end-begin时候大于3941*2：
//若大于，maxData,minData
//若小于，maxData=minData


/* 通过调用DataClass::findMaxMinData(int begin, int end)函数；
 * 可以找到指定范围内数据的最大值和最小值数组，从而获得包络线
 * 而当begin与end之间的数据个数>3941*2的时候，进行取样处理
 * 而当begin与end之间的数据个数<3941*2的时候，将所有数据进行绘制
 *
 * ！！！这里的3940*4是可以更改的，在DataClass::findMaxMinData(int begin, int end)函数中的if语句中修改
*/


DataClass::DataClass(QObject *parent) : QObject(parent)
{

}


/*函数名称：txt2dataV
 * 函数功能：读取选取的二进制文件，然后把其中的数据存储到dataVector数组中
*/
void DataClass::txt2dataV(char *filePath){                   //这里的计算公式需要随时修改

    bool gpsCache[10];
    float canshu = 100.0;

    unsigned char data[16384];

    indexP=indexR=indexGPS=index0=index1=index2=index3=index4=index5=
            index6=index7=index8=index9=indexA=indexB=0;

    int flagPitch,flagRoll;

    // 以读模式打开文件
    ifstream infile;
    infile.open(filePath, ios::in | ios::binary);
    while (1) {
        if(!infile.read((char *)&data, sizeof(data))){
            break;
        }
        for (int i=0;i<8192;i++) {
            if (data[2*i+1]<16) {
                dataVector0[index0] = data[2*i+1]*256 + data[2*i];
                index0++;
            }else if (data[2*i+1]<32) {
                dataVector1[index1] = (data[2*i+1]-16)*256 + data[2*i];
                index1++;
            }else if (data[2*i+1]<48) {
                dataVector2[index2] = (data[2*i+1]-32)*256 + data[2*i];
                index2++;
            }else if (data[2*i+1]<64) {
                dataVector3[index3] = (data[2*i+1]-48)*256 + data[2*i];
                index3++;
            }else if (data[2*i+1]<80) {
                dataVector4[index4] = (data[2*i+1]-64)*256 + data[2*i];
                index4++;
            }else if (data[2*i+1]<96) {
                dataVector5[index5] = (data[2*i+1]-80)*256 + data[2*i];
                index5++;
            }
        }

        if(!infile.read((char *)&data, sizeof(data))){
            break;
        }
        for (int i=0;i<8192;i++) {
            if (data[2*i+1]<16) {
                dataVector6[index6] = data[2*i+1]*256 + data[2*i];
                index6++;
            }else if (data[2*i+1]<32) {
                dataVector7[index7] = (data[2*i+1]-16)*256 + data[2*i];
                index7++;
            }else if (data[2*i+1]<48) {
                dataVector8[index8] = (data[2*i+1]-32)*256 + data[2*i];
                index8++;
            }else if (data[2*i+1]<64) {
                dataVector9[index9] = (data[2*i+1]-48)*256 + data[2*i];
                index9++;
            }else if (data[2*i+1]<80) {
                dataVectorA[indexA] = (data[2*i+1]-64)*256 + data[2*i];
                indexA++;
            }else if (data[2*i+1]<96) {
                dataVectorB[indexB] = (data[2*i+1]-80)*256 + data[2*i];
                indexB++;
            }
        }
    }
    // 关闭打开的文件
    infile.close();
    //qDebug()<<"file over";


    /*改变采样率只影响串口信息的采集，和时间的确定。对其他通道的信息没有影响*/

    //下面对dataVectorB中的数据提取字节到gpsCache中
    int ininin = 1;//1000
    while(ininin<indexB){
        if((dataVectorB[ininin]<2000)&(dataVectorB[ininin+2]<2000)){//dataVectorB
            //读52个点，前50个点，每10个点取均值。舍掉后两个点
            for (int ii=0;ii<10;ii++) {
                int sumData=0;
                for (int inde=0;inde<4;inde++) {
                    sumData+=dataVectorB[ininin+inde];
                }
                if(sumData/4>2000){
                    gpsCache[ii] = 1;
                }else {
                    gpsCache[ii] = 0;
                }
                ininin+=4;
                if(ii==5){
                    ininin++;
                }
            }
            gpsInformation[indexGPS] = gpsCache[1]+gpsCache[2]*2+gpsCache[3]*4+gpsCache[4]*8+
                    gpsCache[5]*16+gpsCache[6]*32+gpsCache[7]*64+gpsCache[8]*128;
            qDebug()<<gpsCache[1]<<gpsCache[2]<<gpsCache[3]<<gpsCache[4]
                    <<gpsCache[5]<<gpsCache[6]<<gpsCache[7]<<gpsCache[8];
            indexGPS++;
        }
        ininin++;
    }

    qDebug()<<"indexGPS="<<indexGPS;

    //下面将gpsCache中的字节进行挑选，并将数据存储到pitch和roll中
    for (int indInf = 0;indInf<indexGPS;indInf++) {
        //qDebug()<<gpsInformation[indInf];
//        if(((gpsInformation[indInf+1]+gpsInformation[indInf+2]+gpsInformation[indInf+3]+
//                gpsInformation[indInf+4]+gpsInformation[indInf+5]+gpsInformation[indInf+6]+
//                gpsInformation[indInf+7]+gpsInformation[indInf+8]+gpsInformation[indInf+9]+
//                gpsInformation[indInf+10]+gpsInformation[indInf+11]+gpsInformation[indInf+12])%256==
//                gpsInformation[indInf+13])&(gpsInformation[indInf]==0x77)){
        if(gpsInformation[indInf]==119){
            //校验和检查完毕
            if(gpsInformation[indInf+4]/16){
                flagPitch = -1;
            }else {
                flagPitch = 1;
            }

            if(gpsInformation[indInf+7]/16){
                flagRoll = -1;
            }else {
                flagRoll = 1;
            }

            pitch[indexP] = ((gpsInformation[indInf+4]%16)*100+gpsInformation[indInf+5]/16*10+
                    gpsInformation[indInf+5]%16+
                    (gpsInformation[indInf+6]/16*10+gpsInformation[indInf+6]%16)/canshu)*flagPitch;
            //qDebug()<<"pitch="<<pitch[indexP];
            indexP++;

            roll[indexR] = ((gpsInformation[indInf+7]%16)*100+gpsInformation[indInf+8]/16*10+
                    gpsInformation[indInf+8]%16+
                    (gpsInformation[indInf+9]/16*10+gpsInformation[indInf+9]%16)/canshu)*flagRoll;
//            qDebug()<<"roll="<<roll[indexR];
//            qDebug()<<"119?="<<gpsInformation[indInf]<<"132?="<<gpsInformation[indInf+3];
            qDebug()<<"pitch小数="<<gpsInformation[indInf+6]<<"roll小数="<<gpsInformation[indInf+9];
            qDebug()<<"pitch整数="<<gpsInformation[indInf+5]<<"roll整数="<<gpsInformation[indInf+8];
            qDebug()<<"sum="<<(gpsInformation[indInf+1]+gpsInformation[indInf+2]+
                    gpsInformation[indInf+3]+gpsInformation[indInf+4]+gpsInformation[indInf+5]+
                    gpsInformation[indInf+6]+gpsInformation[indInf+7]+gpsInformation[indInf+8]+
                    gpsInformation[indInf+9]+gpsInformation[indInf+10]+gpsInformation[indInf+11]+
                    gpsInformation[indInf+12])%256<<"校验和="<<gpsInformation[indInf+13];
//            qDebug()<<gpsInformation[indInf]<<gpsInformation[indInf+1]<<gpsInformation[indInf+2]<<
//                    gpsInformation[indInf+3]<<gpsInformation[indInf+4]<<gpsInformation[indInf+5]<<
//                    gpsInformation[indInf+6]<<gpsInformation[indInf+7]<<gpsInformation[indInf+8]<<
//                    gpsInformation[indInf+9]<<gpsInformation[indInf+10]<<gpsInformation[indInf+11]<<
//                    gpsInformation[indInf+12]<<gpsInformation[indInf+13];
            indexR++;
            indInf+=13;
        }
    }
}


/*函数名称：initializeMaxMin
 * 函数功能：初始化maxData和minData两个数组，清空所有数据
*/
void DataClass::initializeMaxMin(){//!!!!!这里的0需要修改
    //置零
    memset(pitchCa, 0, 1000*sizeof(float));
    memset(rollCa, 0, 1000*sizeof(float));

}


void DataClass::findMaxMinData(bool *flag){

    initializeMaxMin();

    if(flag[11]){
        for (int i=0;i<indexP;i++) {
            pitchCa[i] = pitch[i];
        }
    }else{
        for (int i=0;i<indexP;i++) {
            pitchCa[i] = 0;
        }
    }
    if(flag[12]){
        for (int i=0;i<indexP;i++) {
            rollCa[i] = roll[i];
        }
    }else{
        for (int i=0;i<indexP;i++) {
            rollCa[i] = 0;
        }
    }
}
